const responseMessage = require("../response/response_message");
const path = require("path");
const fs = require("fs");
const config = require("../config")
const {
  addDynamicApi,
  getNewDynamicNumApi,
  getFriendsDynamicApi,
  getDynamicApi,
  updateDynamicsReadStatus,
} = require("../papi/dynamic_api");

exports.add_dynamic = async (ctx) => {
  try {
    const { dynamic_imgs } = ctx.request.files;
    ctx.request.body.dynamic_imgs = [];
    if(!(dynamic_imgs instanceof Array) && dynamic_imgs){
      const reader = fs.createReadStream(dynamic_imgs.filepath);
      let filePath = path.join(__dirname, "../uploads") + `/${dynamic_imgs.newFilename}${path.extname(dynamic_imgs.originalFilename)}`;
      ctx.request.body.dynamic_imgs.push(config.uploadUrl+`${dynamic_imgs.newFilename}${path.extname(dynamic_imgs.originalFilename)}`)
      const upStream = fs.createWriteStream(filePath);
      reader.pipe(upStream);
    }else{
     if(dynamic_imgs){
      for (let file of dynamic_imgs) {
        const reader = fs.createReadStream(file.filepath);
        let filePath = path.join(__dirname, "../uploads") + `/${file.newFilename}${path.extname(file.originalFilename)}`;
        ctx.request.body.dynamic_imgs.push(config.uploadUrl+`${file.newFilename}${path.extname(file.originalFilename)}`)
        const upStream = fs.createWriteStream(filePath);
        reader.pipe(upStream);
      }
     }
    }
     const dynamic = ctx.request.body;
     const dynamicResult = await addDynamicApi(dynamic);
     if (dynamicResult) {
     ctx.body = new responseMessage(201, "success", dynamicResult);
     } else {
     ctx.body = new responseMessage(201, "添加失败");
     }
  } catch (error) {
    ctx.body = new responseMessage(500, error.message, {});
  }
};

exports.get_new_dynamic_num = async (ctx) => {
  try {
    const { _id } = ctx.request.query;
    const result = await getNewDynamicNumApi(_id);
    ctx.body = new responseMessage(201, "success", result);
  } catch (error) {
    ctx.body = new responseMessage(500, error.message, {});
  }
};

exports.get_friends_dynamic = async (ctx) => {
  try {
    const { _id } = ctx.request.query;
    const result = await getFriendsDynamicApi(_id);
    ctx.body = new responseMessage(201, "success", result);
  } catch (error) {
    ctx.body = new responseMessage(500, error.message, {});
  }
};

exports.get_dynamic = async (ctx) => {
  try {
    const { _id } = ctx.request.query;
    const result = await getDynamicApi(_id);
    ctx.body = new responseMessage(201, "success", result);
  } catch (error) {
    ctx.body = new responseMessage(500, error.message, {});
  }
};

exports.update_dynamics_read_status = async (ctx) => {
  try {
    const { _id } = ctx.request.body;
    const { modifiedCount } = await updateDynamicsReadStatus(_id);
    if (modifiedCount > 0) {
      ctx.body = new responseMessage(201, "success", {});
    } else {
      ctx.body = new responseMessage(201, "此条记录已经是已读状态了", {});
    }
  } catch (error) {
    ctx.body = new responseMessage(500, error.message, {});
  }
};
